/*
 * filters.js
 * 
 * Collection of data filter stuff
 * 
 * AUTHORS
 * Nate Kozlowski
 */

 cs441ShibeDoge.selectFilter = function() {
	//does nothing
	var gender = document.getElementById("gender").value;
	var major = document.getElementById("major").value;
	var ethnicity = document.getElementById("ethnicity").value;
	var gpaL = document.getElementById("gpaLower").value;
	var gpaU = document.getElementById("gpaUpper").value;
	
	//fixing the GPAs
	if (gpaL != '') {
		if (gpaL < 0) {
			gpaL = 0;
		} else if (gpaL > 4) {
			gpaL = 4;
		}
	} else {
		gpaL = 0;
	}
	
	if (gpaU != '') {
		if (gpaU < 0) {
			gpaU = 0;
		} else if (gpaU > 4) {
			gpaU = 4;
		}
	} else {
		gpaU = 4;
	}
	
	//swapping if they mixed the values up
	if (gpaU < gpaL) {
		var temp = gpaU;
		gpaU = gpaL;
		gpaL = temp;
	}
	
	//gpa = 3, gender = 20, major = 22, ethnicity = 18
	var majorCode = cs441ShibeDoge.codeTranslator.majorCode[major];
	var ethnicityCode = cs441ShibeDoge.codeTranslator.ethnicityCode[ethnicity];
	var genderCode;
	if (gender == 'Male') {
		genderCode = 'M';
	} else if (gender == 'Female') {
		genderCode = 'F';
	}

	var newDataSet = cs441ShibeDoge.data;
	cs441ShibeDoge.oldRows = cs441ShibeDoge.data.rows.slice(0);
	var oldRows = cs441ShibeDoge.data.rows.slice(0);
	var newRows = new Array();

	//Remove any data not in the selected gender
	if (genderCode || !(typeof majorCode === 'undefined') ||  !(typeof ethnicityCode === 'undefined') || (gpaU != 4 || gpaL != 0)) {
		
		
		if (cs441ShibeDoge.dataObject.REGION_TYPE == cs441ShibeDoge.CITY_SELECTED) {
			var tempRows = new Array();
			var city = document.getElementById("city").value;
			var state = document.getElementById("state").value;
			
			//alert(city + ', ' + state + ' selected');
			//Remove any data not in the selected region
			for (var i = 0; i < oldRows.length; i++) {
				if (oldRows[i][14] == city && oldRows[i][15] == state) {
					tempRows.push(oldRows[i]);
				}
			}
			oldRows = tempRows;
		}
		if (genderCode && !(typeof majorCode === 'undefined') && !(typeof ethnicityCode  === 'undefined')) {
		//	alert('gender & major & ethnicity changed');
			for (var i = 0; i < oldRows.length; i++) {
				if (oldRows[i][20] == genderCode && oldRows[i][22] == majorCode && oldRows[i][18] == ethnicityCode && (oldRows[i][3] >= gpaL && oldRows[i][3] <= gpaU)) {
					newRows.push(oldRows[i]);
				}
			}
		} else if (genderCode && !(typeof majorCode === 'undefined')) {
		//	alert('gender & major changed');
			for (var i = 0; i < oldRows.length; i++) {
				if (oldRows[i][20] == genderCode && oldRows[i][22] == majorCode && (oldRows[i][3] >= gpaL && oldRows[i][3] <= gpaU)) {
					newRows.push(oldRows[i]);
				}
			}
		} else if (!(typeof ethnicityCode === 'undefined') && !(typeof majorCode === 'undefined')) {
		//	alert('ethnicity & major changed');
			for (var i = 0; i < oldRows.length; i++) {
				if (oldRows[i][18] == ethnicityCode && oldRows[i][22] == majorCode && (oldRows[i][3] >= gpaL && oldRows[i][3] <= gpaU)) {
					newRows.push(oldRows[i]);
				}
			}
		} else if (genderCode && !(typeof ethnicityCode === 'undefined')) {
		//	alert('gender & ethnicity changed');
			for (var i = 0; i < oldRows.length; i++) {
				if (oldRows[i][20] == genderCode && oldRows[i][18] == ethnicityCode && (oldRows[i][3] >= gpaL && oldRows[i][3] <= gpaU)) {
					newRows.push(oldRows[i]);
				}
			}
		} else if (genderCode) {
		//	alert('gender changed');
			for (var i = 0; i < oldRows.length; i++) {
				if (oldRows[i][20] == genderCode && (oldRows[i][3] >= gpaL && oldRows[i][3] <= gpaU)) {
					newRows.push(oldRows[i]);
				}
			}
		} else if (!(typeof majorCode === 'undefined')) {
		//	alert('major changed');
			for (var i = 0; i < oldRows.length; i++) {
				if (oldRows[i][22] == majorCode && (oldRows[i][3] >= gpaL && oldRows[i][3] <= gpaU)) {
					newRows.push(oldRows[i]);
				}
			}
		} else if (!(typeof ethnicityCode === 'undefined')) {
		//	alert('ethnicity changed');
			for (var i = 0; i < oldRows.length; i++) {
				if (oldRows[i][18] == ethnicityCode && (oldRows[i][3] >= gpaL && oldRows[i][3] <= gpaU)) {
					newRows.push(oldRows[i]);
				}
			}
		} else {
			//gpa
		//	alert('gpa changed');
			for (var i = 0; i < oldRows.length; i++) {
				if ((oldRows[i][3] >= gpaL && oldRows[i][3] <= gpaU)) {
					newRows.push(oldRows[i]);
				}
			}
		}
		
		newDataSet.rows = newRows;
		
		//Update the table accordingly
		cs441ShibeDoge.calculateDataFields(newDataSet);
		cs441ShibeDoge.currentDataSet = newDataSet;
		//Show the "show all applicants" button
		//document.getElementById("showAll").style.display = 'block';
		//see if you can switch this to a disabled button
		document.getElementById("filterSelectButton").style.display = 'none';
		document.getElementById("filterResetButton").style.display = 'inline-block';
	}
	
	//alert('Filter button pressed! Values: \nGender: ' + gender + '\nMajor: ' + major + '\n Ethnicity: ' + ethnicity + '\nGPA Lower: ' + gpaL + '\nGPA Upper: ' + gpaU + '\nmajor code: ' + majorCode + '\nEthnicity code: ' + ethnicityCode + '\ngender code: ' + genderCode);
 }
 
 cs441ShibeDoge.resetFilter = function() {
	document.getElementById("gender").value = 'all';
	document.getElementById("major").value = 'all';
	document.getElementById("ethnicity").value = 'all';
	document.getElementById("gpaLower").value = 0;
	document.getElementById("gpaUpper").value = 4;
	document.getElementById("filterResetButton").style.display = 'none';
	document.getElementById("filterSelectButton").style.display = 'inline-block';
	//cs441ShibeDoge.selectFilter();
	cs441ShibeDoge.data.rows = cs441ShibeDoge.oldRows;
	cs441ShibeDoge.calculateDataFields(cs441ShibeDoge.data);
	cs441ShibeDoge.currentDataSet = cs441ShibeDoge.data;
	
	//newDataSet = cs441ShibeDoge.allApplicantRows.slice(0);
	//Update the table accordingly
	//cs441ShibeDoge.calculateDataFields(newDataSet);
	//cs441ShibeDoge.currentDataSet = newDataSet;
	
	//var newDataSet = cs441ShibeDoge.data.slice(0);
 }
 
 /*
 * populateGenderSelectDropDown
 * 
 * Populates the gender select dropdown
 */
cs441ShibeDoge.populateGenderSelectDropDown = function(data) {

	//Stores data to help us parse the dropdown menus, such as whether or not
	//a city/state has previously been seen and lists of cities in a particular
	//state
	var dropDownEntryStorageGender = {};
	dropDownEntryStorageGender.genderList = ["Male", "Female"];

	//Parse the options into an HTML string and write it to the document
	var dropDownOptions = "<option value='all'>Show Data for both genders</option>\n";
	for (var i = 0; i < dropDownEntryStorageGender.genderList.length; i++) {
		dropDownOptions += "<option value='" + dropDownEntryStorageGender.genderList[i] + "'>" + dropDownEntryStorageGender.genderList[i] + "</option>\n";
	}
	document.getElementById("gender").innerHTML = dropDownOptions;

	//Cache the data for later use
	cs441ShibeDoge.dropDownData.gender = dropDownEntryStorageGender;
	genderList = ["Male", "Female"]
}

cs441ShibeDoge.populateMajorSelectDropDown = function(data) {

	//Stores data to help us parse the dropdown menus, such as whether or not
	//a city/state has previously been seen and lists of cities in a particular
	//state
	var dropDownEntryStorageMajor = {};
	dropDownEntryStorageMajor.majorList = ["Undecided", 
										"Japanese Studies", 
										"Biology", 
										"Bio-Chemistry", 
										"General Science", 
										"Allied Health Science", 
										"Health Care Management", 
										"Science Communication", 
										"Gerontology", 
										"Business Admin:Undeclared", 
										"Business Administration", 
										"Accounting", 
										"Management", 
										"Marketing", 
										"Finance", 
										"Economics", 
										"Business Computer Systems", 
										"Communication", 
										"Communication", 
										"Journalism", 
										"Management Communication", 
										"Communication Management", 
										"Computer Science", 
										"Computer Applications Managmnt", 
										"Education (MED)", 
										"Elementary Education", 
										"Secondary Education", 
										"Counseling/Psychology", 
										"Student Development", 
										"Student Services Administratn", 
										"Counseling", 
										"Special Education", 
										"Music Education", 
										"Administration of Justice", 
										"Peace Studies", 
										"History", 
										"Political Science", 
										"Sociology", 
										"History & Government", 
										"Criminal Justice", 
										"Society & Justice", 
										"Society & Justice", 
										"Theology", 
										"Interdisciplinary", 
										"Undeclared", 
										"Pre-Communication", 
										"Nursing: Adult Health", 
										"Nursing: Community Hlth", 
										"Chemistry: ACS", 
										"Chemistry: Biochemistry", 
										"Life Science", 
										"Education (MA)", 
										"Education (MAT)", 
										"Non-Matriculating", 
										"Continuing Education", 
										"Education", 
										"Engineering, General", 
										"Civil Egr:Civil Track", 
										"Elec Egr:Electrical Track", 
										"Mechanical Engineering", 
										"Electrical Engineering (MSEE)", 
										"Engineering Management", 
										"Elec Egr:Computer Track", 
										"Engineering Science", 
										"Industrial Engineering", 
										"Fine Arts", 
										"Music", 
										"Drama", 
										"Theater Management", 
										"Modern Languages", 
										"French", 
										"German", 
										"Spanish", 
										"Japanese", 
										"Nursing", 
										"Nursing -RN/BSN/MS", 
										"Medical Technology", 
										"English", 
										"Speech", 
										"Philosophy", 
										"Library Science", 
										"Mathematics", 
										"Physical Science", 
										"Physics", 
										"Chemistry", 
										"Engineering Chemistry", 
										"Life Science", 
										"Psychology", 
										"Social Work", 
										"Social Service", 
										"Organizational Communication", 
										"Pastoral Ministry", 
										"Mathematics", 
										"Environmental Science", 
										"Envir Ethics & Policy", 
										"Nursing: Leadrshp Hlth Sys", 
										"Nursing: Family Nrs Pract", 
										"Post Master's FNP Program", 
										"Nursing: Adult Nrs Pract", 
										"Civil Egr:Environmental Track", 
										"Marketing and Management", 
										"Global Business", 
										"Religious Education", 
										"Education", 
										"HPE: Sport Exercise & Fitness", 
										"Engineering", 
										"College Student Personnel", 
										"Educational Administration", 
										"Reading Education", 
										"Nursing Educator Specialty", 
										"Post Master's Nursing Ed", 
										"German Studies", 
										"Social Justice", 
										"General Studies (BS)", 
										"General Studies (BA)", 
										"Clinical Nurse Leader", 
										"French Studies", 
										"Entrepreneurship", 
										"Nursing", 
										"Sociology:Criminal Justice", 
										"ESOL", 
										"Zoology", 
										"Community Health & Admin", 
										"Maternal & Child Nursing", 
										"Childrearing & Family Nursing", 
										"Innovative Ldrship & Mgmt", 
										"Psych/Mental Hlth Nrs Teaching", 
										"Operations Technology Mgmt", 
										"DNP Doctor of Nursing Practice", 
										"Catholic Studies"];

	//Parse the options into an HTML string and write it to the document
	var dropDownOptions = "<option value='all'>Show Data for all majors</option>\n";
	for (var i = 0; i < dropDownEntryStorageMajor.majorList.length; i++) {
		dropDownOptions += "<option value='" + dropDownEntryStorageMajor.majorList[i] + "'>" + dropDownEntryStorageMajor.majorList[i] + "</option>\n";
	}
	document.getElementById("major").innerHTML = dropDownOptions;

	//Cache the data for later use
	cs441ShibeDoge.dropDownData.major = dropDownEntryStorageMajor;
	majorList = ["Undecided", 
					"Japanese Studies", 
					"Biology", 
					"Bio-Chemistry", 
					"General Science", 
					"Allied Health Science", 
					"Health Care Management", 
					"Science Communication", 
					"Gerontology", 
					"Business Admin:Undeclared", 
					"Business Administration", 
					"Accounting", 
					"Management", 
					"Marketing", 
					"Finance", 
					"Economics", 
					"Business Computer Systems", 
					"Communication", 
					"Communication", 
					"Journalism", 
					"Management Communication", 
					"Communication Management", 
					"Computer Science", 
					"Computer Applications Managmnt", 
					"Education (MED)", 
					"Elementary Education", 
					"Secondary Education", 
					"Counseling/Psychology", 
					"Student Development", 
					"Student Services Administratn", 
					"Counseling", 
					"Special Education", 
					"Music Education", 
					"Administration of Justice", 
					"Peace Studies", 
					"History", 
					"Political Science", 
					"Sociology", 
					"History & Government", 
					"Criminal Justice", 
					"Society & Justice", 
					"Society & Justice", 
					"Theology", 
					"Interdisciplinary", 
					"Undeclared", 
					"Pre-Communication", 
					"Nursing: Adult Health", 
					"Nursing: Community Hlth", 
					"Chemistry: ACS", 
					"Chemistry: Biochemistry", 
					"Life Science", 
					"Education (MA)", 
					"Education (MAT)", 
					"Non-Matriculating", 
					"Continuing Education", 
					"Education", 
					"Engineering, General", 
					"Civil Egr:Civil Track", 
					"Elec Egr:Electrical Track", 
					"Mechanical Engineering", 
					"Electrical Engineering (MSEE)", 
					"Engineering Management", 
					"Elec Egr:Computer Track", 
					"Engineering Science", 
					"Industrial Engineering", 
					"Fine Arts", 
					"Music", 
					"Drama", 
					"Theater Management", 
					"Modern Languages", 
					"French", 
					"German", 
					"Spanish", 
					"Japanese", 
					"Nursing", 
					"Nursing -RN/BSN/MS", 
					"Medical Technology", 
					"English", 
					"Speech", 
					"Philosophy", 
					"Library Science", 
					"Mathematics", 
					"Physical Science", 
					"Physics", 
					"Chemistry", 
					"Engineering Chemistry", 
					"Life Science", 
					"Psychology", 
					"Social Work", 
					"Social Service", 
					"Organizational Communication", 
					"Pastoral Ministry", 
					"Mathematics", 
					"Environmental Science", 
					"Envir Ethics & Policy", 
					"Nursing: Leadrshp Hlth Sys", 
					"Nursing: Family Nrs Pract", 
					"Post Master's FNP Program", 
					"Nursing: Adult Nrs Pract", 
					"Civil Egr:Environmental Track", 
					"Marketing and Management", 
					"Global Business", 
					"Religious Education", 
					"Education", 
					"HPE: Sport Exercise & Fitness", 
					"Engineering", 
					"College Student Personnel", 
					"Educational Administration", 
					"Reading Education", 
					"Nursing Educator Specialty", 
					"Post Master's Nursing Ed", 
					"German Studies", 
					"Social Justice", 
					"General Studies (BS)", 
					"General Studies (BA)", 
					"Clinical Nurse Leader", 
					"French Studies", 
					"Entrepreneurship", 
					"Nursing", 
					"Sociology:Criminal Justice", 
					"ESOL", 
					"Zoology", 
					"Community Health & Admin", 
					"Maternal & Child Nursing", 
					"Childrearing & Family Nursing", 
					"Innovative Ldrship & Mgmt", 
					"Psych/Mental Hlth Nrs Teaching", 
					"Operations Technology Mgmt", 
					"DNP Doctor of Nursing Practice", 
					"Catholic Studies"]
}

cs441ShibeDoge.populateEthnicitySelectDropDown = function(data) {

	//Stores data to help us parse the dropdown menus, such as whether or not
	//a city/state has previously been seen and lists of cities in a particular
	//state
	var dropDownEntryStorageEthnicity = {};
	dropDownEntryStorageEthnicity.ethnicityList = ["Undeclared",
											"Caucasian",
											"African American",
											"Hispanic",
											"Asian Pac Islander",
											"Alaskan Native/Am Indian",
											"Other",
											"Native Hawaiian"];

	//Parse the options into an HTML string and write it to the document
	var dropDownOptions = "<option value='all'>Show Data for all Ethnicities</option>\n";
	for (var i = 0; i < dropDownEntryStorageEthnicity.ethnicityList.length; i++) {
		dropDownOptions += "<option value='" + dropDownEntryStorageEthnicity.ethnicityList[i] + "'>" + dropDownEntryStorageEthnicity.ethnicityList[i] + "</option>\n";
	}
	document.getElementById("ethnicity").innerHTML = dropDownOptions;

	//Cache the data for later use
	cs441ShibeDoge.dropDownData.ethnicity = dropDownEntryStorageEthnicity;
	ethnicityList = ["Undeclared",
						"Caucasian",
						"African American",
						"Hispanic",
						"Asian Pac Islander",
						"Alaskan Native/Am Indian",
						"Other",
						"Native Hawaiian"]
}